package server.object;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;

import server.diffusion.MulticastDiffusion;
import server.diffusion.MulticastDiffusionImpl;

public class CObject3DImpl extends UnicastRemoteObject implements CObject3D {

	private static final long serialVersionUID = -4618632780664511706L;
	private AObject3D abstraction;
	private MulticastDiffusion multicastDiffuser;
	
	public CObject3DImpl(AObject3D model) throws RemoteException{
		super();
		abstraction = model;
		try {
			String ipHouse = "192.168.1.12";
			String ipIfsic = "148.60.13.83";			
			String adresse = "rmi://"+ipIfsic+"/universe/"+abstraction.getName();
			Naming.bind(adresse, this);
			System.out.println(adresse);
			multicastDiffuser = MulticastDiffusionImpl.instance("224.98.13.67", 1099);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public String getName() throws RemoteException{
		return abstraction.getName();
	}

	@Override
	public Quat4d getOrientation() throws RemoteException{
		return abstraction.getOrientation();
	}

	@Override
	public Vector3d getPosition() throws RemoteException{
		return abstraction.getPosition();
	}

	@Override
	public void setGeometry(String geometry) throws RemoteException{
		abstraction.setGeometry(geometry);
	}

	@Override
	public void setName(String name) throws RemoteException{
		abstraction.setName(name);
	}

	@Override
	public void setOrientation(Quat4d orientation) throws RemoteException{
		abstraction.setOrientation(orientation);
	}

	@Override
	public void setPosition(Vector3d position) throws RemoteException{
		abstraction.setPosition(position);
	}
	
	@Override
	public void move(Vector3d movement) throws RemoteException{
		abstraction.move(movement);
		multicastDiffuser.diffuseMessage(getName(), getPosition(), getOrientation(), getGeometry());
	}

	@Override
	public String getGeometry() throws RemoteException {
		return abstraction.getGeometry();
	}

}
